﻿using FairyGUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using WgFrame.Net;

public class MainPanel : BasePanel
{
    private GTextField tfPlayerName;
    private GTextField tfPlayerXiuWei;
    private GTextField tfGold;
    private GTextField tfRmb;
    private GList lsMenu;
    private GComponent panelFight;
    private GButton btnSocial;

    private PackPage packPage;
    private HeroPage heroPage;
    private ShopPage shopPage;

    private FightMode fightMode;

    public override void Init()
    {
        base.Init();

        tfPlayerName = this.view.GetChild("n12").asTextField;
        tfPlayerXiuWei = this.view.GetChild("n14").asTextField;
        tfGold = this.view.GetChild("n17").asTextField;
        tfRmb = this.view.GetChild("n19").asTextField;
        panelFight = view.GetChild("n11").asCom;
        panelFight.GetChild("n4").asButton.onClick.Add(OnBuZhenClick);
        btnSocial = this.view.GetChild("n37").asButton;
        btnSocial.draggable = true;
        btnSocial.dragBounds = new Rect(this.view.x, this.view.y, this.view.width, this.view.height);
        btnSocial.onClick.Add(OnSocialClick);

        lsMenu = view.GetChild("n35").asList;
        lsMenu.onClickItem.Add(OnMenuClickItem);

        packPage = new PackPage(view.GetChild("n36").asCom);
        heroPage = new HeroPage(view.GetChild("n33").asCom);
        shopPage = new ShopPage(view.GetChild("n38").asCom);

        fightMode = new FightMode(this.view.GetChild("n11").asCom);
    }

    public override void Show(params object[] para)
    {
        MsgListenerMgr.AddMsgListener(MsgListenerType.NewSocialMsg, OnNewSocialMsg);
        MsgListenerMgr.AddMsgListener(MsgListenerType.StartFight, OnStartFightMsg);
        MsgListenerMgr.AddMsgListener(MsgListenerType.BaseInfoChange, OnBaseInfoChange);

        OnBaseInfoChange(para);

        lsMenu.selectedIndex = 0;
        OnMenuClickItem(null);
        base.Show();
    }

    public override void Close()
    {
        MsgListenerMgr.RemoveMsgListener(MsgListenerType.NewSocialMsg, OnNewSocialMsg);
        MsgListenerMgr.RemoveMsgListener(MsgListenerType.StartFight, OnStartFightMsg);
        MsgListenerMgr.RemoveMsgListener(MsgListenerType.BaseInfoChange, OnBaseInfoChange);

        base.Close();
    }

    public override void Dispose()
    {
        base.Dispose();
    }

    protected override GComponent CreateView()
    {
        return UIPackage.CreateObject("Main", "panel_main").asCom;
    }

    private void OnSocialClick(EventContext context)
    {
        PopupPanelManager.Open<SocialPanel>();
        btnSocial.GetTransition("t0").Stop();
    }

    private void OnBuZhenClick(EventContext context)
    {
        PopupPanelManager.Open<BuZhenPanel>();
    }

    private void OnMenuClickItem(EventContext context)
    {
        if (lsMenu.selectedIndex == 0)
        {
            heroPage.Show();
            packPage.Close();
            shopPage.Close();
        }
        else if (lsMenu.selectedIndex == 1)
        {
            packPage.Show();
            heroPage.Close();
            shopPage.Close();
        }
        else if (lsMenu.selectedIndex == 2)
        {
            shopPage.Show();
            heroPage.Close();
            packPage.Close();
        }
    }

    private void OnNewSocialMsg(object[] para)
    {
        if (!PopupPanelManager.Get<SocialPanel>().IsVisible)
        {
            btnSocial.GetTransition("t0").Play(-1, 0, null);
        }
    }

    private void OnStartFightMsg(object[] para)
    {
        FightItem[] rightTeam = (FightItem[])para[0];
        FightItem[] leftTeam = (FightItem[])para[1];
        fightMode.Start(rightTeam, leftTeam);
    }

    private void OnBaseInfoChange(object[] para)
    {
        tfPlayerName.text = GameNetwork.Player.Name;
        tfPlayerXiuWei.text = CommonUtil.NumberToString(GameNetwork.Player.BaseInfo.XiuWei);
        tfGold.text = CommonUtil.NumberToString(GameNetwork.Player.BaseInfo.Gold);
        tfRmb.text = CommonUtil.NumberToString(GameNetwork.Player.BaseInfo.Rmb);
    }

    public class FightMode
    {
        private readonly GComponent view;
        private readonly FightManager fightManager;

        private GComponent[] cptLeftTeam = new GComponent[ConfigUtil.FIGHT_MEMBER_COUNT];
        private GComponent[] cptRightTeam = new GComponent[ConfigUtil.FIGHT_MEMBER_COUNT];
        private GTextField tfBout;

        private float interval = 0.81f;

        public FightMode(GComponent view)
        {
            this.view = view;
            fightManager = new FightManager();
            tfBout = this.view.GetChild("n7").asTextField;

            cptLeftTeam[0] = this.view.GetChild("n14").asCom;
            cptLeftTeam[1] = this.view.GetChild("n15").asCom;
            cptLeftTeam[2] = this.view.GetChild("n16").asCom;
            cptLeftTeam[3] = this.view.GetChild("n10").asCom;
            cptLeftTeam[4] = this.view.GetChild("n11").asCom;
            cptLeftTeam[5] = this.view.GetChild("n12").asCom;

            cptRightTeam[0] = this.view.GetChild("n23").asCom;
            cptRightTeam[1] = this.view.GetChild("n24").asCom;
            cptRightTeam[2] = this.view.GetChild("n25").asCom;
            cptRightTeam[3] = this.view.GetChild("n20").asCom;
            cptRightTeam[4] = this.view.GetChild("n21").asCom;
            cptRightTeam[5] = this.view.GetChild("n22").asCom;
            Init();
        }

        public void Start(FightItem[] rightTeam, FightItem[] leftTeam)
        {
            Init();
            fightManager.Init();

            tfBout.text = string.Format("{0}/{1}", 1, FightManager.MAX_BOUT_COUNT);
            for (int i=0; i< ConfigUtil.FIGHT_MEMBER_COUNT; i++)
            {
                if (rightTeam[i] == null)
                {
                    continue;
                }

                InitComponent(cptRightTeam[i], rightTeam[i]);
                fightManager.AddRightTeam(rightTeam[i], i);
            }

            for (int i = 0; i < ConfigUtil.FIGHT_MEMBER_COUNT; i++)
            {
                if (leftTeam[i] == null)
                {
                    continue;
                }

                InitComponent(cptLeftTeam[i], leftTeam[i]);
                fightManager.AddLeftTeam(leftTeam[i], i);
            }
            fightManager.Run();
            Timers.inst.Add(interval, 0, OnPlay);
        }

        public void OnPlay(object param)
        {
            if (fightManager.FightRecords.Count == 0)
            {
                Timers.inst.Remove(OnPlay);
                return;
            }


            FightRecordItem fightRecord = fightManager.FightRecords[0];
            fightManager.FightRecords.RemoveAt(0);

            tfBout.text = string.Format("{0}/{1}", fightRecord.BoutCount, FightManager.MAX_BOUT_COUNT);
            GComponent atkComponent;
            if (fightRecord.Atk.TeamType == TeamType.Left)
            {
                atkComponent = cptLeftTeam[fightRecord.Atk.Idx];
                atkComponent.GetTransition("t_l1").Play();
            }
            else
            {
                atkComponent = cptRightTeam[fightRecord.Atk.Idx];
                atkComponent.GetTransition("t_r1").Play();
            }

            atkComponent.GetChild("n4").asProgress.value = fightRecord.AtkMp;


            for (int i=0; i< fightRecord.AtkEffectList.Count; i++)
            {
                AtkEffect atkEffect = fightRecord.AtkEffectList[i];

                GComponent piaozhiCpt;
                GComponent component;
                if (atkEffect.Target.TeamType == TeamType.Right)
                {
                    component = cptRightTeam[atkEffect.Target.Idx];
                    piaozhiCpt = UIPackage.CreateObject("Main", "text_fight2").asCom;
                }
                else
                {
                    component = cptLeftTeam[atkEffect.Target.Idx];
                    piaozhiCpt = UIPackage.CreateObject("Main", "text_fight1").asCom;
                }
                GRoot.inst.AddChild(piaozhiCpt);
                GTextField gTextField = piaozhiCpt.GetChild("n0").asTextField;

                if (atkEffect.eType == AtkEffectType.Dodge)
                {
                    gTextField.text = "*";
                }
                else if (atkEffect.eType == AtkEffectType.LoseHpByBaoJi)
                {
                    gTextField.text = "#-" + atkEffect.Value.ToString();
                }
                else
                {
                    gTextField.text = "-"+atkEffect.Value.ToString();
                }
                piaozhiCpt.x = component.x + component.width / 2 - piaozhiCpt.width / 2;
                piaozhiCpt.y = component.y + 20;

                piaozhiCpt.GetTransition("t0").Play(()=> {
                    GRoot.inst.RemoveChild(piaozhiCpt);
                });
                UpdateTargetInfo(component, atkEffect);
            }
        }

        private void Init()
        {
            for (int i = 0; i < cptLeftTeam.Length; i++)
            {
                cptLeftTeam[i].visible = false;
            }

            for (int i = 0; i < cptRightTeam.Length; i++)
            {
                cptRightTeam[i].visible = false;
            }
        }

        private void InitComponent(GComponent component, FightItem fightItem)
        {
            component.visible = true;
            GTextField tfName = component.GetChild("n2").asTextField;
            tfName.text = fightItem.Name;
            tfName.color = CommonUtil.GetHeroPinZhiColor(fightItem.PinZhi);

           
            GTextField tfDujie = component.GetChild("n5").asTextField;
            if (fightItem.DujieLv > 0)
            {
                tfDujie.text = CommonUtil.GetDuJieName(fightItem.DujieLv);
                tfDujie.color = CommonUtil.GetHeroDuJieColor(fightItem.DujieLv);
            }
            else
            {
                tfDujie.visible = false;
            }

            GTextField tfHp = component.GetChild("n6").asTextField;
            tfHp.text = string.Format("{0}/{1}", CommonUtil.NumberToString(fightItem.HP), CommonUtil.NumberToString(fightItem.TotalHp));

            GProgressBar pbHp = component.GetChild("n3").asProgress;
            pbHp.value = 100;

            GProgressBar pbMp = component.GetChild("n4").asProgress;
            pbMp.value = fightItem.MP;
        }

        private void UpdateTargetInfo(GComponent component, AtkEffect atkEffect)
        {
            GTextField tfHp = component.GetChild("n6").asTextField;
            tfHp.text = string.Format("{0}/{1}", CommonUtil.NumberToString(atkEffect.CurrHp), CommonUtil.NumberToString(atkEffect.Target.TotalHp));

            float hp1 = atkEffect.CurrHp;
            float hp2 = atkEffect.Target.TotalHp;
            GProgressBar pbHp = component.GetChild("n3").asProgress;
            pbHp.value = hp1 / hp2 * 100;

            GProgressBar pbMp = component.GetChild("n4").asProgress;
            pbMp.value = atkEffect.CurrMp;
        }
    }
}
